/**
 * @description 函数防抖，闭包版
 */
export function debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result

    const later = function() {
        // 据上一次触发时间间隔
        const last = +new Date() - timestamp

        // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
        if (last < wait && last > 0) {
            timeout = setTimeout(later, wait - last)
        } else {
            timeout = null
            // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
            if (!immediate) {
                result = func.apply(context, args)
                if (!timeout) context = args = null
            }
        }
    }

    return function(...args) {
        context = this
        timestamp = +new Date()
        const callNow = immediate && !timeout
        // 如果延时不存在，重新设定延时
        if (!timeout) timeout = setTimeout(later, wait)
        if (callNow) {
            result = func.apply(context, args)
            context = args = null
        }

        return result
    }
}

const DEBOUNCE = { // 防抖函数任务控制
    task: null
};
/**
 * 函数防抖
 * @param func 执行的任务
 * @param wait 等待时间
 * @param options 函数参数
 */
export function debounceNotClosure (func, wait, ...options) {
    if (typeof func !== 'function') {
        throw new Error('【参数类型错误】func参数只能接受function类型');
    }
    wait = +wait;
    if (isNaN(wait)) {
        throw new Error('【参数类型错误】wait只能是number，或者能被转换成number的字符串');
    }
    if (DEBOUNCE.task) {
        clearTimeout(DEBOUNCE.task);
        DEBOUNCE.task = null;
    } else {
        DEBOUNCE.task = setTimeout(function () {
            func.call(this, ...options);
        }.bind(this), wait);
    }
}

const THROTTLE_TIME = { // 防抖函数时间控制
    lastTime: 0
};
/**
 * 函数节流
 * @param func 执行的任务
 * @param delay 延迟时间
 * @param options 函数参数
 */
export function throttle (func, delay, ...options) {
    if (typeof func !== 'function') {
        throw new Error('【参数类型错误】func参数只能接受function类型');
    }
    delay = +delay;
    if (isNaN(delay)) {
        throw new Error('【参数类型错误】wait只能是number，或者能被转换成number的字符串');
    }
    const now = Date.now();
    const diff = now - THROTTLE_TIME.lastTime;
    if (diff < delay) return;
    func.call(this, ...options);
    THROTTLE_TIME.lastTime = now;
}
/**
 * 数据类型
 */
const isArrayFn = (data, type) => {
    let dataType = Object.prototype.toString.call(data);
    dataType = dataType.toLowerCase();
    return dataType == ('[object ' + type + ']');
}
/**
 * 旭日图data转化
 */
export function sunburstData(data, names, styles) {
    let text = new Array(names.length).fill();
    let temp = [];
    function getIndex(temp, value) {
        let count = -1;
        temp.map((item, index) => {
            if (item.name == value) {
                count = index;
            }
        })
        return count;
    }
    const fun = (temp, item, nameIndex = 0) => {
        if (nameIndex >= names.length) return;
        if (!isArrayFn(text[nameIndex], 'array')) {
            text[nameIndex] = [];
        }
        let index = (temp && temp.length) ? getIndex(temp, item[names[nameIndex]]) : -1;
        if (index < 0) {
            if (nameIndex == (names.length - 1)) {
                temp.push({
                    value: item[names[nameIndex]],
                    name: item[names[nameIndex]],
                    itemStyle: styles[nameIndex]
                });
            } else {
                temp.push({
                    name: item[names[nameIndex]],
                    children: new Array(),
                    itemStyle: styles[nameIndex]
                });
                fun(temp[(temp.length - 1)].children, item, nameIndex + 1);
            }
        } else {
            if (nameIndex >= names.length) return;
            fun(temp[index].children, item, nameIndex + 1);
        }
    };
    data.map(item => {
        fun(temp, item);
    });
    return temp;
}
/**
 * 得到日期
 */
export function getDay(time = (new Date())) {
    let thisDate = new Date(time);
    return thisDate.getFullYear() + '-' + (thisDate.getMonth() + 1) + '-' + thisDate.getDate();
}
/**
 * 得到每月第一天
 */
export function getMonthsFirstDay(startDate ,endDate) {
    startDate = new Date(startDate);
    endDate = new Date(endDate);
    let startDateObj={
        y:startDate.getFullYear(),
        m:(startDate.getMonth() + 1),
        d:startDate.getDate()
    };
    let endDateObj={
        y:endDate.getFullYear(),
        m:(endDate.getMonth() + 1),
        d:endDate.getDate()
    };
    let res=[];
    if(startDateObj.d==1){
        res.push(startDateObj.y+'/'+startDateObj.m+'/'+'1');
    }
    while(startDateObj.y<endDateObj.y||(startDateObj.y==endDateObj.y&&startDateObj.m<endDateObj.m)){
        if(startDateObj.m==12){
            startDateObj.m=1;
            startDateObj.y++;
        }else{
            startDateObj.m++;
        }
        res.push(startDateObj.y+'/'+startDateObj.m+'/'+'1');
    }
    return res;
}
/**
 * 得到区间数组
 */
export function getRangeArr(startNum ,endNum,step=1) {
    let res=[];
    let num=startNum;
    res.push(num);
    while(num<endNum){
        num+=step;
        res.push(num<endNum?num:endNum);
    }
    return res;
}
